package com.gaff.emp.core.modules.perm.controller.menu;

import com.gaff.emp.core.EmpException;
import com.gaff.emp.core.R;
import com.gaff.emp.core.modules.auth.annotation.Auth;
import com.gaff.emp.core.modules.perm.controller.menu.form.*;
import com.gaff.emp.core.modules.perm.entity.SysMenu;
import com.gaff.emp.core.modules.perm.service.SysMenuService;
import com.gaff.emp.core.syslog.annotation.SysOperaLog;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


@Slf4j
@RestController
@RequestMapping("/sys/menu")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    @GetMapping("/all")
    @ApiOperation("获取全部菜单列表")
    @SysOperaLog(value = "获取全部菜单列表", isPersistence = false)
    @Auth(perm = "menu")
    public R all() {
        return R.ok(sysMenuService.queryAllMenus());
    }

    @PostMapping("/createCatalog")
    @ApiOperation("新增目录")
    @SysOperaLog(value = "新增目录")
    @Auth(perm = "menu")
    public R createCatalog(@RequestBody @Valid CreateCatalogForm form, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new IllegalArgumentException(bindingResult.getFieldError().getDefaultMessage());
        }
        sysMenuService.createCatalog(form);
        return R.ok();
    }

    @PostMapping("/createMenu")
    @ApiOperation("新增菜单")
    @SysOperaLog(value = "新增菜单")
    @Auth(perm = "menu")
    public R createMenu(@RequestBody @Valid CreateMenuForm form, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new IllegalArgumentException(bindingResult.getFieldError().getDefaultMessage());
        }
        sysMenuService.createMenu(form);
        return R.ok();
    }

    @PostMapping("/updateMenu")
    @ApiOperation("更新菜单")
    @SysOperaLog(value = "更新菜单")
    @Auth(perm = "menu")
    public R updateMenu(@RequestBody @Valid UpdateMenuForm form, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new IllegalArgumentException(bindingResult.getFieldError().getDefaultMessage());
        }
        sysMenuService.updateMenu(form);
        return R.ok();
    }

    @GetMapping("/info/{id}")
    @ApiOperation("获取菜单信息")
    @SysOperaLog(value = "获取菜单信息", isPersistence = false)
    @Auth(perm = "menu")
    public R info(@PathVariable("id") Integer id) {
        SysMenu menu = sysMenuService.queryMenuById(id);
        if (menu == null) {
            throw new EmpException("菜单不存在");
        }
        return R.ok(menu);
    }

    @PostMapping("/delete")
    @ApiOperation("删除菜单")
    @SysOperaLog(value = "删除菜单")
    @Auth(perm = "menu")
    public R delete(@RequestBody @Valid DeleteForm form, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new IllegalArgumentException(bindingResult.getFieldError().getDefaultMessage());
        }
        sysMenuService.deleteById(form.getId());
        return R.ok();
    }

}
